/*
 * BlueLogger.java
 *
 * Created on 3 aprile 2007, 19.01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bloomingstars.bluepad.util;

import javax.microedition.lcdui.Form;

/**
 *
 * @author vega
 */
public class BlueLogger
{    
    public static final int ERROR   = 3;
    public static final int WARNING = 2;
    public static final int INFO    = 1;
    public static final int FINE    = 0;
    public static final int MUTE    = 100;
            
    private BlueLogger()
    {
    }
    
    public static void setLevel(int level)
    {
        logLevel = level;
    }
    
    public static int getLevel()
    {
        return logLevel;
    }
    
    public static void setForm(Form f)
    {
        fapp.setForm(f);
    }
    
    /**
     * Choose append mode. Can be either <code>CONSOLE</code>, <code>FORM</code>
     * or a combination of both by or'ing flag values
     */
    public static void setAppendMode(int mode)
    {
        appendToConsoleFlag = (mode & 0x1) == BlueAppender.CONSOLE;
        appendToFormFlag    = (mode & 0x2) == BlueAppender.FORM;
    }
    
    /**
     * Log message if call level is greater than of equal to the current
     * class level
     * 
     * @param level log level
     * @param msg string to log
     */   
    public static synchronized void log(int level, String msg)
    {
        if (level >= logLevel)
        {
            if (appendToConsoleFlag)
            {
                capp.append(msg);
            }

            if (appendToFormFlag)
            {
                fapp.append(msg);
            }            
        }
    }
    
    public static void error(String s)
    {
        log(ERROR, s);
    }
    
    public static void warning(String s)
    {
        log(WARNING, s);
    }
    
    public static void info(String s)
    {
        log(INFO, s);
    }
    
    public static void fine(String s)
    {
        log(FINE, s);
    }
    
    /* Application log level */
    private static int logLevel = MUTE;
   
    private static boolean appendToConsoleFlag;
   
    private static boolean appendToFormFlag;
    
    private static ConsoleAppender capp = new ConsoleAppender();
    private static FormAppender    fapp = new FormAppender();
}
